package com.ujson.chat.client.builder;

import com.ujson.chat.client.dto.ChatInputDto;
import com.ujson.chat.client.session.ClientSession;
import com.ujson.chat.common.proto.MessageProto;
import io.netty.channel.Channel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <h3>ujson-parent</h3>
 * <p>登录消息创建器</p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-23 21:48
 */
@Slf4j
@Service
public class ChatMessageBuilder extends AbstractBuilder {

    @Override
    public MessageProto.MessageType getType() {
        return MessageProto.MessageType.MESSAGE_REQUEST;
    }

    public static enum MsgType {
        // 消息类型  1：纯文本  2：音频 3：视频 4：地理位置 5：其他
        TEXT(1),
        AUDIO(2),
        VIDEO(3),
        LOCATION(4),
        OTHER(5);
        private int type;
        private MsgType(int type) {
            this.type = type;
        }

        public int getType() {
            return type;
        }
    }
    @Data
    public static class MessageExt {
        private MsgType msgType;
        private String url;// 多媒体地址
        private String property;// 附加属性
        private String fromNick;// 发送方Nick昵称
        private String json;// 附加json
    }

    public MessageProto.Message build(Channel channel, ChatInputDto chat, MessageExt msgExt) {
        MessageProto.MessageRequest messageRequest =
                MessageProto.MessageRequest.newBuilder()
                        .setMsgId(nextSequence())
                        .setFrom(ClientSession.getClientSession(channel).getUserDto().getUsername())
                        .setTo(chat.getTo())
                        .setTime(System.currentTimeMillis())
                        .setContent(chat.getMessage())
                        .setMsgType(msgExt.getMsgType().getType())
                        .build();
        return commonBuild(channel).toBuilder()
                .setMessageRequest(messageRequest)
                .build();
    }
}
